//
//  JMServiceController.m
//  NewiOSProject
//
//  Created by li on 2023/9/13.
//  Copyright © 2023 范智渊. All rights reserved.
//

#import "JMServiceController.h"

#import "LayoutView.h"

#import "JMPickerSelectionView.h"
#import "JMPickerSelectionModel.h"
#import "PaddingView.h"
#import "LayoutScrollView.h"
#import "CustomStackView.h"
#import "CustomStackView.h"
#import "LayoutView.h"
#import "UIView+PopupProtocol.h"
#import "JMAuthentiactionConfirmView.h"
#import "JMServiceListModel.h"
@interface JMServiceController ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
@property(nonatomic, strong) UIStackView *rootStack;
@property(nonatomic, strong) LayoutScrollView *scrollView;
@property(nonatomic, strong) JMAuthentiactionConfirmView *confirmView;
@property(nonatomic, strong) NSMutableArray<JMServiceListModel *> *list;
@end

@implementation JMServiceController



- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.type == 1) {
        self.TitleLabel.text = @"Servicio al cliente";
    }else if (self.type == 2) {
        self.TitleLabel.text = @"Línea de reclamación";
    }
    [self requestData];
    self.view.backgroundColor = [UIColor whiteColor];
}


- (void)setupViews {
    
    [self.view addSubview:self.rootStack];
    [self.rootStack addArrangedSubview:self.scrollView];
    self.scrollView.rootStack.contentView.spacing = 0;
    
    
    UIImageView *topBgV = [UIImageView new];
    [topBgV.widthAnchor constraintEqualToConstant:kAppW()].active = YES;
    [topBgV.heightAnchor constraintEqualToConstant:kAppW()*108/375.0].active = YES;
    [topBgV setImage:ImageNamed(@"bjt-wd-kf")];
    [self.scrollView.rootStack addContentView:topBgV];
    
    UILabel *titlab = [JMControl createLabelWithFrame:(CGRectMake(24, 24, kAppW(), 24)) Font:mediumFont(17) Text:@"Hola, bienvenido a llamar." color:WhiteColor()];
    [topBgV addSubview:titlab];
    
    UILabel *subTitlab = [JMControl createLabelWithFrame:(CGRectMake(24, titlab.bottom+12, kAppW(), 24)) Font:mediumFont(17) Text:@"Servicio al cliente." color:WhiteColor()];
    [topBgV addSubview:subTitlab];
    
    NSArray *imgNames = @[@"ICON／电话",@"ICON／you xiang",@"Group 8767"];
    NSArray *titles = @[@"Teléfono",@"Buzón de correo",@"Whats App"];
    
    for(NSInteger i = 0; i < self.list.count; i ++) {
        JMServiceListModel *model = self.list[i];
        
        UIView *contentView = [UIView new];
        
        contentView.layer.cornerRadius = 20;
        contentView.layer.masksToBounds = YES;
        [contentView.widthAnchor constraintEqualToConstant:kScreenWidth].active = YES;
        [contentView.heightAnchor constraintEqualToConstant:138].active = YES;
        [self.scrollView.rootStack addContentView:contentView];
        
        UIImageView *icon = [UIImageView new];
        icon.translatesAutoresizingMaskIntoConstraints = NO;
        [contentView addSubview:icon];
        [icon setImage:ImageNamed(imgNames[i])];
        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(24);
            make.top.mas_equalTo(12);
            make.width.height.mas_equalTo(24);
        }];
        
        
        UILabel *title = [UILabel new];
        title.text = titles[i];
        title.font = [UIFont systemFontOfSize:14];
        title.textColor = [UIColor colorWithHexString:@"#0F1511"];
        title.numberOfLines = 0;
        title.translatesAutoresizingMaskIntoConstraints = NO;
        [contentView addSubview:title];
        [NSLayoutConstraint activateConstraints:@[
            [title.leftAnchor constraintEqualToAnchor:icon.rightAnchor constant:12],
            [title.centerYAnchor constraintEqualToAnchor:icon.centerYAnchor constant:0],
            [title.heightAnchor constraintEqualToConstant:24],
        ]];
        
        UILabel *subTitle = [UILabel new];
        subTitle.text = model.title;
        subTitle.font = [UIFont systemFontOfSize:14];
        subTitle.textColor = [UIColor colorWithHexString:@"#0F1511"];
        subTitle.numberOfLines = 0;
        subTitle.translatesAutoresizingMaskIntoConstraints = NO;
        [contentView addSubview:subTitle];
        [NSLayoutConstraint activateConstraints:@[
            [subTitle.leftAnchor constraintEqualToAnchor:icon.leftAnchor constant:0],
            [subTitle.topAnchor constraintEqualToAnchor:icon.bottomAnchor constant:12],
            [subTitle.heightAnchor constraintEqualToConstant:24],
        ]];
        
        UILabel *contact = [UILabel new];
        contact.text = model.contact;
        contact.font = [UIFont systemFontOfSize:16 weight:(UIFontWeightMedium)];
        contact.textColor = [UIColor colorWithHexString:@"#0F1511"];
        contact.translatesAutoresizingMaskIntoConstraints = NO;
        contact.numberOfLines = 0;
        [contentView addSubview:contact];
        [contact sizeToFit];
        [NSLayoutConstraint activateConstraints:@[
            [contact.leftAnchor constraintEqualToAnchor:icon.leftAnchor constant:0],
            [contact.topAnchor constraintEqualToAnchor:subTitle.bottomAnchor constant:22],
            [contact.rightAnchor constraintEqualToAnchor:subTitle.rightAnchor constant:0],
            [contact.heightAnchor constraintEqualToConstant:24],
        ]];
        //
        UIView *lineV = [[UIView alloc]init];
        lineV.backgroundColor = mainSepartorColor();
        [contentView addSubview:lineV];
        [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(contentView);
            make.top.mas_equalTo(contact.mas_bottom).offset(16);
            make.height.mas_equalTo(1);
        }];
        
        if (model.type == 1) {
            [contentView addTapGestuer:^(UIView * _Nonnull view) {
                callNumber(model.contact);
                if (self.type==1) {
                    [JMCommonMethod uploadStatistics:@"CustomerService" actionType:@"Click" parameters:nil];//@{@"productAndRanks":productAndRanks}
                }else{
                    [JMCommonMethod uploadStatistics:@"Complaint" actionType:@"Click" parameters:nil];//@{@"productAndRanks":productAndRanks}
                }
            }];
        }else if (model.type == 2) {
            [contentView addTapGestuer:^(UIView * _Nonnull view) {
                [JMCommonMethod uploadStatistics:@"CustomerService" actionType:@"Click" parameters:nil];//@{@"productAndRanks":productAndRanks}
                [JMProgressHelper toastInWindowWithMessage:@"Copia éxito"];
                [UIPasteboard generalPasteboard].string = model.contact;
            }];
            
        }else if (model.type == 3) {
            subTitle.hidden = YES;
            [contact mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(icon.mas_bottom).offset(26);
                make.left.equalTo(icon.mas_left).offset(26);
            }];
            [contentView addTapGestuer:^(UIView * _Nonnull view) {
                [UIPasteboard generalPasteboard].string = model.contact;
                [JMProgressHelper toastInWindowWithMessage:@"Copia éxito"];
            }];
        }
        
        [contentView layoutIfNeeded];
    }
    if (self.type==1) {
        UIView *contV = [UIView new];
        [contV.widthAnchor constraintEqualToConstant:kScreenWidth - 48].active = YES;
        [contV.heightAnchor constraintEqualToConstant:126].active = YES;
        [self.scrollView.rootStack addContentView:contV];
        
        UILabel *botLab = [JMControl createLabelWithFrame:(CGRectZero) Font:Font(10) Text:@"Por favor envíanos su nombre y apellido, número telefónico y nombre de la aplicación cuando refleja cualquier realimentación para poder verificar los casos, gracias!" color:Hex_Str_COLOR(@"#0F1511")];
        
        [JMControl changeTextWithLabel:botLab changeStr:@"número telefónico y nombre de la aplicación" font:fontWithWeight(10, UIFontWeightRegular) andColor:Hex_Str_COLOR(@"#EC533E")];
        botLab.textAlignment = 1;
        [contV addSubview:botLab];
        [botLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(contV);
            make.width.equalTo(@(kAppW()-48));
            make.top.bottom.equalTo(contV);
        }];
    }
    
    
    UIButton *reading = [UIButton buttonWithType:(UIButtonTypeCustom)];
    reading.titleLabel.font = [UIFont systemFontOfSize:12];
    [reading setTitle:@"《 Politica de privacidad 》" forState:(UIControlStateNormal)];
    [reading setTitleColor: [UIColor colorWithHexString:@"#222222"] forState:(UIControlStateNormal)];
    [reading.heightAnchor constraintEqualToConstant:36].active = YES;
    [reading addTarget:self action:@selector(viewPrivacyAgreement:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.rootStack addArrangedSubview: [PaddingView initWithContentView:reading edgeInsets:(UIEdgeInsetsMake(20, 0, 64, 0))]];
}

- (void)viewPrivacyAgreement: (UIButton *)sender {
    [JMCommonMethod presentWebviewWithUrl:serviceUrlWith(loginPrivacyAgreement) andTitle:@"Politica de privacidad"];
}

- (void)requestData {
    
    NSString *url = @"/api/common/customerServiceInfo";
    if (self.type == 2) {
        url = @"/api/common/getComplaintPhone";
    }
    
    
    [[JMBaseRequest sharedInstance] requestBasePost:BasePostRequestUrl(url) params:@{} success:^(NSDictionary * _Nullable dataDic, NSArray * _Nullable dataArray) {
        
        if (dataDic != nil) {
            if (self.type == 1) {
                NSString *phone = (NSString *)[dataDic safeStringForKey:@"phone"];
                NSString *email = (NSString *)[dataDic safeStringForKey:@"email"];
                NSString *whatsApp = (NSString *)[dataDic safeStringForKey:@"whatsApp"];
                
                for (NSInteger i = 0; i < 3; i++) {
                    JMServiceListModel *model = [JMServiceListModel new];
                    if (i == 0) {
                        model.title = @"Llame a la línea directa de servicio al cliente";
                        model.type = 1;
                        model.contact = phone;
                    }else if (i == 1) {
                        model.title = @"Correo electrónico de servicio al cliente";
                        model.type = 2;
                        model.contact = email;
                    }else if (i == 2) {
                        model.title = @"Whats APP";
                        model.type = 3;
                        model.contact = whatsApp;
                    }
                    [self.list addObject:model];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self setupViews];
                });
            }else {
                NSString *phone = (NSString *)dataDic;
                JMServiceListModel *model = [JMServiceListModel new];
                
                model.title = @"Línea de reclamación:";
                model.type = 1;
                model.contact = [NSString stringWithFormat:@"%@", phone];
                
                [self.list addObject:model];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self setupViews];
                });
            }
            
        }
    } fail:^(NSString * _Nonnull errorMsg) {
        
        
    }];
    
}

#pragma mark - 懒加载
- (UIStackView *)rootStack {
    if (!_rootStack) {
        self.rootStack = [UIStackView new];
        _rootStack.alignment = UIStackViewAlignmentCenter;
        _rootStack.axis = UILayoutConstraintAxisVertical;
        _rootStack.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:_rootStack];
        
        [self.rootStack mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(@(NAVIGATION_BAR_HEIGHT()));
        }];
    }
    return  _rootStack;
}
- (LayoutScrollView *)scrollView {
    if (!_scrollView) {
        self.scrollView = [LayoutScrollView new];
        _scrollView.leftMargin.constant = 0;
        _scrollView.rootStack.contentView.axis = UILayoutConstraintAxisVertical;
        [_scrollView.widthAnchor constraintEqualToConstant: kScreenWidth].active = YES;
        _scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.rootStack.layoutViewAlignment = LayoutViewAlignmentTop;
    }
    return _scrollView;
}

- (JMAuthentiactionConfirmView *)confirmView {
    if (!_confirmView) {
        self.confirmView = [JMAuthentiactionConfirmView new];
        _confirmView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [_confirmView.confirmBut addTarget:self action:@selector(confirmAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _confirmView;
}


#pragma mark - 懒加载
- (NSMutableArray *)list {
    if (!_list) {
        self.list = [NSMutableArray array];
    }
    return _list;
}

@end
